let yid = 18
let yfree=''
let ytext=''
let yfinish=''
let ydate=''
let ypopu=''
let k=1
let counts=0
const tipshow = document.querySelector('.tipshow')
const tab1 = document.querySelectorAll('.tabs p')
tab1.forEach((n,colors) => {
    console.log(n)
    if(colors==0){
        n.style.color='#ff4c2f'
    }
})
const total = document.querySelector('.total')
function totals(event) {
    event.stopPropagation()
    const tipshow = document.querySelector('.tipshow')
    tipshow.style.display = 'block'
}
document.onclick = () => {
    tipshow.style.display = 'none';
};
function popular(event) {
    total.innerHTML = event.target.innerHTML
    total.style.color = '#ff4c2f'
    event.target.parentNode.style.display = 'none'
    tab1.forEach(m => {
        m.style.color = ''
    })
    if(event.target.innerHTML=="周人气"){
        ypopu=1
        booktab(yid,k,ytext,yfree,yfinish,ydate,1)
    }else if(event.target.innerHTML=="月人气"){
        ypopu=2
        booktab(yid,k,ytext,yfree,yfinish,ydate,2)
    }else if(event.target.innerHTML=="总人气"){
        ypopu=3
        booktab(yid,k,ytext,yfree,yfinish,ydate,3)
    }
}
function tab(event) {
    tab1.forEach(i => {
        i.style.color = ''
    })
    event.target.style.color = '#ff4c2f'
    total.style.color = ''
    if(event.target.innerHTML=="更新时间"){
        console.log(111)
        booktab(yid,k,ytext,yfree,yfinish,ydate,5)
        ypopu=5
    }else if(event.target.innerHTML=="推荐票"){
        booktab(yid,k,ytext,yfree,yfinish,ydate,4)
        ypopu=4
    }else if(event.target.innerHTML=="字数"){
        booktab(yid,k,ytext,yfree,yfinish,ydate,6)
        ypopu=6
    }
}
const booktab = (a,k,b,c,d,g,h,sort,updateDateType,finish,free,words) => {
    axios.post(
        'https://app2.motie.com/pc/books/search',
        {},
        {
            params: {
                pageNo: k,
                pageSize: 20,
                categoryId: a,
                sort: h,
                ...(b !== undefined && b !== null && b !== '' ? { words: b } : {}),
                ...(c !== undefined && c !== null && c !== '' ? { free: c } : {}),
                ...(d !== undefined && d !== null && d !== '' ? { finish: d } : {}),
                ...(g !== undefined && g !== null && g !== '' ? { updateDateType: g } : {}),
                
            }
        }
    ).then(res => {
        console.log(res.data.data.paginate.totalItemsCount)
        counts=res.data.data.paginate.totalItemsCount||0
        const work=document.querySelector('.work')
        work.innerHTML='共'+counts+'本作品'
        const ul = document.querySelector('.content2 ul')
        ul.innerHTML=''
        res.data.data.items.forEach(co => {
            const date = new Date(co.lastChapterTime);
            const year = date.getFullYear();
            const month = date.getMonth() + 1; // 月份从0开始，需+1
            const day = date.getDate();
            const hours = date.getHours();
            const minutes = date.getMinutes();
            const seconds = date.getSeconds();
            // 补零函数：小于10的数字前加0
            const padZero = (num) => num < 10 ? '0' + num : num;
            // 格式化时间
            const newDate = `${year}-${padZero(month)}-${padZero(day)} ${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`;

            let name1 = (co.categoryVO != null && co.categoryVO != undefined) ? co.categoryVO.name : ''
            ul.innerHTML += `
            <li>
                <img onclick="jumps(${co.bookId}); return false;"  src="${co.bookIconUrl}" alt="">
                <div class="con-right">
                    <div onclick="jumps(${co.bookId}); return false;" >${co.bookName}</div>
                    <p>
                        <span>${co.authorName}</span> | <span>${name1}</span> |${co.finish == true ? `<span>已完结</span>` : `<span>连载中</span>`} 
                    </p>
                    <p class="info">
                        ${co.introduction}
                    </p>
                    <p><span>${co.words} | </span><span class="fresh">${newDate}更新</span></p>
                </div>
            </li>
        `
        })
        layui.use(['laypage'], function() {
            const laypage = layui.laypage;
            laypage.render({
                elem: 'demo-laypage-all',
                theme: '#ff4c2f',
                count: counts, // 实时总条数
                limit: 20,
                curr: k, // 当前页码
                layout: ['count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj, first) {
                    if (!first) {
                        k = obj.curr; 
                        booktab(yid, obj.curr, ytext, yfree, yfinish, ydate, ypopu);
                    }
                }
            });
        });
    }).catch(err => {
        console.log(err)
    })
}
booktab(18, 1, ytext, yfree, yfinish, ydate, ypopu);
function deletes(event){
    event.target.parentNode.remove()
}
function jumps(id){
    if(localStorage.getItem('account')){
        window.location.href=`./laikan-bookDetail.html?id=${id}`
    }else{
        window.location.href='./login.html'
    }
    
}